//
//  HDRootScanVC.m
//  HDWuDaDaoServiceCenter
//
//  Created by HDNiuKuiming on 2017/5/9.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

#import "HDRootScanVC.h"

@interface HDRootScanVC ()
{
    HDDeclare *declare;
    CGAffineTransform _captureSizeTransform;
}
@property (strong, nonatomic) IBOutlet UIView *scanRectView;
@property (weak, nonatomic) IBOutlet UIView *animateBgView;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;

@property (nonatomic, strong) UIImageView * line;
@property (nonatomic, strong) NSTimer * timer;

@property (nonatomic, strong) NSString *museumID;

@end

@implementation HDRootScanVC

#pragma mark
#pragma mark -------- Init Methods --------

- (void)viewDidLoad {
    [super viewDidLoad];
    declare = [HDDeclare sharedDeclare];

    [self.bgView bringSubviewToFront:self.scanRectView];
    [self.bgView bringSubviewToFront:self.scanRectView];
    [self.bgView bringSubviewToFront:_tipLabel];
}

- (IBAction)pushToQRCodeInputAcgion:(id)sender {
    [self performSegueWithIdentifier:@"QRCodePagePushToInputLine" sender:nil];
}


-(void)animation1
{
    CGFloat originY = 0;
    CGFloat originX = _line.frame.origin.x;
    CGFloat width = _line.frame.size.width;
    
    if (upOrdown == NO) {
        num ++;
        _line.frame = CGRectMake(originX, originY+2*num, width, 2);
        if (2*num == _animateBgView.frame.size.height) {
            upOrdown = YES;
        }
    }
    else {
        num --;
        _line.frame = CGRectMake(originX, originY+2*num, width, 2);
        if (num == 0) {
            upOrdown = NO;
        }
    }
}

- (void)startScanAnimate
{
    dispatch_async(dispatch_get_main_queue(), ^{
        _line = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, _animateBgView.frame.size.width-40, 2)];
        _line.contentMode = UIViewContentModeScaleToFill;
        _line.image = [UIImage imageNamed:@"QRCode_line"];
        [self.animateBgView addSubview:_line];
        
        _timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation1) userInfo:nil repeats:YES];
    });
}

#pragma mark
#pragma mark -------- QRCode Methods --------

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{}

#pragma mark
#pragma mark -------- Dealloc Methods --------

- (void)scanSuccessBack
{
    [self cameraClean];
    [self.navigationController popViewControllerAnimated:NO];
    [self performSelectorOnMainThread:@selector(postNotiAction) withObject:nil waitUntilDone:YES];
}

- (void)postNotiAction
{
    [[NSNotificationCenter defaultCenter] postNotificationName:kRoot_Scan_Success_Notification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)backActin:(id)sender {
    [self cameraClean];
    [super back];
}

- (void)cameraClean
{
    [_timer invalidate];
    self.timer = nil;
    self.line = nil;
}

- (void)dealloc
{
    [self cameraClean];
}


@end
